package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.bac.BacAntClassDto;
import com.ysd.lis.entity.bac.BacInterpretationCodeDict;
import com.ysd.lis.entity.bac.BacSensitiveResultRule;
import com.ysd.lis.mapper.bac.BacSensitiveResultRuleMapper;
import com.ysd.lis.request.bac.BacAntClassParam;
import com.ysd.lis.service.bac.BacSensitiveResultRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2024-01-25
 */
@Service
@Slf4j
public class BacSensitiveResultRuleServiceImpl extends ServiceImpl<BacSensitiveResultRuleMapper, BacSensitiveResultRule> implements BacSensitiveResultRuleService {

    @Autowired
    BacSensitiveResultRuleMapper bacSensitiveResultRuleMapper;

    @Override
    public Result findSensitiveResultRuleList(BacAntClassParam param) {
        MPJLambdaWrapper<BacSensitiveResultRule> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacSensitiveResultRule.class)
                .selectAs(BacInterpretationCodeDict::getName, BacSensitiveResultRule::getSensitiveNa)
                .leftJoin(BacInterpretationCodeDict.class, "sa",
                        p -> p.eq(BacInterpretationCodeDict::getCode, BacSensitiveResultRule::getSensitiveNo).eq(BacInterpretationCodeDict::getDelFlag, 0));
        queryWrapper.eq(BacSensitiveResultRule::getDelFlag, 0)
                .orderByAsc(BacSensitiveResultRule::getSeq);
        List<BacSensitiveResultRule> ruleList = bacSensitiveResultRuleMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", ruleList);
    }

    @Override
    public Result saveOrUpdateSensitiveResultRule(BacAntClassDto dto) {
        List<BacSensitiveResultRule> bacSensitiveResultRuleList = dto.getBacSensitiveResultRuleList();
        boolean b = this.saveOrUpdateBatch(bacSensitiveResultRuleList);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result batchDeleteSensitiveResultRule(BacAntClassDto dto) {
        List<BacSensitiveResultRule> bacSensitiveResultRuleList = dto.getBacSensitiveResultRuleList();
        List<String> ids = bacSensitiveResultRuleList.stream().map(BacSensitiveResultRule::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }
}
